<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        :listData="columns1"
        :status="status"
        :list="$store.state.fullSubGiftSale.giftData"
      ></EditTable>
    </div>
  </div>
</template>
<script>
import EditTable from "../edit/ChoicePresentTable";
import { columns1 } from "./ChoicePreventData";
import {getValueArray, setEmptyArray, getSelectStatus, getValueStatus} from "./ChoiceCommidyMethods"
import {compareSaleGift, getCode, setArrayValue, setAdd, setExchangeValue, onJoinSaleBody, isPriceEmptyItem, isPriceEmpty, onSet} from "@/views/sales/common/TableSaleMethods"
export default {
  name: "choicecommidy",
  components: {
    EditTable
  },
  mounted() {
    if (!this.$store.state.fullSubGiftSale.giftData.length) {
      this.getProductData({size: 5000});
    }
  },
  data() {
    return {
      model1: "",
      value: "",
      cityList: [],
      columns1,
      data1: [],
      status: "loading"
    };
  },
  methods: {
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    getProductData(param) {
      let self = this;
      self.status = "loading"
      this.$ajaxGet("/activity/product/spu_sku_page", param).then(data => {self.$store.state.fullSubGiftSale.giftData = self.data1 = data.records;
        setAdd(self, self.data1, {index: "checked", val1: false, childName: "productSkuList" })
        setAdd(self, self.data1, {index: "showChildren", val1: false, childName: "productSkuList" })
        setAdd(self, self.data1, {index: "buttonChecked", val1: false, childName: "productSkuList"})
        setAdd(self, self.data1, {index: "returnPrice", val1: "", childName: "productSkuList"})
        setAdd(self, self.data1, {index: "quantity", val1: "", childName: "productSkuList"})
        getCode (self, self.data1, {index: 'parentcode', childName: "productSkuList"})
        self.status = "pass";
        compareSaleGift(self.$store.state.fullSubGiftSale.giftData, self.$store.state.fullSubGiftSale.GetgiftData)
      })
    },
    getSaleData(type) {
      if (type !== "buttonChecked") {
        if (isPriceEmpty(this.$store.state.fullSubGiftSale.giftData, {index: "returnPrice", index1: "checked",childName: "productSkuList"})) {
          this.$Message.error("退货价不能为空")
          return false
        }
      }
      this.$store.commit("getfullSubGiftPreventData", type);
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.data1, {
        index: "checked",
        val1: true,
        childName: "productSkuList"
      }): setArrayValue(this.data1, {
        index: "checked",
        val1: false,
        childName: "productSkuList"
      });
    },
    onJoinSaleBody(item, todo, params) {
      onJoinSaleBody(item, todo, params)
    },
    onJoinSale(item, todo, selectAll) {
      if(isPriceEmptyItem(item, {index: "returnPrice",childName: "productSkuList"})) {
        this.$Message.error("退货价不能为空");
        return false;
      }
      //点击加入促销的时候触发的事件
      item.buttonChecked = !item.buttonChecked;
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' });
      this.onJoinSaleBody(item, todo, {index:'buttonChecked',index1: 'productSkuList' });
      this.$store.commit("deletefullSubGiftPrevent", {todo: todo, item: item})
      this.getSaleData('buttonChecked')
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {index:'checked',index1: 'productSkuList' });
    }
  }
};
</script>

